

import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:smart_freezer/core/model/params_model.dart';
import 'package:smart_freezer/core/model/product_model.dart';
import 'package:smart_freezer/core/resources/app_config.dart';
import 'package:smart_freezer/core/resources/icon_names.dart';
import 'package:smart_freezer/core/resources/routers.dart';
import 'package:smart_freezer/core/service/api/app_api.dart';
import 'package:smart_freezer/core/service/service/result_data.dart';
import 'package:smart_freezer/core/utils/local_storage.dart';
import 'package:smart_freezer/core/utils/logs.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';
import 'package:smart_freezer/core/view/date_picker.dart';
import 'package:smart_freezer/core/view/update_location.dart';
import 'package:smart_freezer/core/view/update_params.dart';
import 'package:smart_freezer/core/view/update_time.dart';
import 'package:ty_smart_home/ty_smart_home.dart';

import '../../../core/utils/color_util.dart';
import '../../../generated/locale_keys.g.dart';

class DeviceSettingParams extends StatefulWidget {
  const DeviceSettingParams({Key? key}) : super(key: key);

  @override
  State<DeviceSettingParams> createState() => _DeviceSettingParamsState();
}

class _DeviceSettingParamsState extends State<DeviceSettingParams> {

  final _productIdList = Get.arguments as List;
  String _productId = "";
  List<ParamsModel> _dataList = [];

  // 获取设备列表
  List<ProductModel> _productModelList = [];

  bool isDarkMode = LocalStorage.getThemeMode() == 1;

  /// 门开关, 智能除霜, 是否使用T2探头, T3探头, 蜂鸣器触发器, 蒸发机控制
  final boolList = ["DS", "ID", "T2", "AR", "FT", "2N", "T3"];

  @override
  void initState() {
    super.initState();

    _productId = _productIdList.first;
    _getParamsList(_productId);

    // 获取涂鸦设备列表
    _getTyDeviceList();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.white,
        elevation: 1,
        leadingWidth: 50,
        leading: InkWell(
          onTap: () {
            Get.back();
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 9, 0, 9),
            child: Icon(Icons.arrow_back_ios_rounded),
          ),
        ),
        title: Text(LocaleKeys.device_info_params_setting.trc(),),
        centerTitle: true,
      ),
      body: Container(
        width: MainCommon.screenWidth,
        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
        child: ListView.builder(
            itemCount: _dataList.length + 1,
            itemBuilder: (context, index){
              final paramsModel = index > 0 ? _dataList[index - 1] : ParamsModel();

              return InkWell(
                onTap: () {
                  if(index > 0) {
                    if(LocalStorage.getRole() == "3" && (paramsModel.customCode.isNotEmpty || _containBoolCode(paramsModel))) { // 只有工程经理才能设置参数, 数据点有配置
                      _dealwithParams(paramsModel);
                    }
                  }
                },
                child: Container(
                  color: isDarkMode ? MainCommon.darkBlueMaterialColor : Colors.white,
                  constraints: BoxConstraints(
                    minHeight: index == 0 ? 40 : 50,
                  ),
                  padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                  child: Column(
                    children: [
                      Container(
                        constraints: BoxConstraints(
                          minHeight: index == 0 ? 39 : 49,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              child: Container(
                                // width: 150,
                                child: Text(index == 0 ? LocaleKeys.device_info_type.trc() : paramsModel.name + "(${paramsModel.code})",
                                  style: TextStyle(
                                    fontSize: index == 0 ? 13 : 16,
                                    fontWeight: index == 0 ? FontWeight.normal : FontWeight.bold,
                                    color: index == 0 ? Colors.grey : (isDarkMode ? Colors.white : Colors.black),
                                  ),),
                              ),
                            ),
                            Row(
                              children: [
                                Container(
                                  width: 70,
                                  child: Text(index == 0 ? LocaleKeys.device_info_params_original.trc() : paramsModel.defaultValue,
                                    style: TextStyle(fontSize: 13, color: Colors.grey), textAlign: TextAlign.right,),
                                ),
                                Container(
                                  width: 90,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Text(index == 0 ? LocaleKeys.device_info_params_current.trc() : paramsModel.value,
                                        style: TextStyle(fontSize: 13, color: Colors.grey), textAlign: TextAlign.right,),
                                      index == 0 ? Container() : (paramsModel.customCode.isNotEmpty || _containBoolCode(paramsModel) ? Icon(Icons.chevron_right, color: Colors.grey,) : Container(width: 15,)),
                                    ],
                                  ),
                                ),

                              ],
                            ),

                          ],
                        ),
                      ),
                      Divider(
                        height: 1,
                        indent: 5,
                        endIndent: 5,
                      ),
                    ],
                  ),
                ),
              );
            }),
      ),
    );
  }

  // 获取设备数据
  _getParamsList(String productId) async {
    _dataList = [];

    final params = {"productId": "${productId}"};
    ResultData data = await AppApi.getInstance().getSettingInfo(params);
    if(data.code == 0){

      final defaultParamsList = data.data["defaultSettingValue"] as List;
      final paramsList = data.data["productSettingValue"] as List;
      defaultParamsList.forEach((defaultEle) {
        paramsList.forEach((ele) {
          final model = ParamsModel();
          final defaultCode = defaultEle["code"];
          final code = ele["code"];
          if(defaultCode == code) {
            model.id = ele["id"];
            model.defaultId = defaultEle["id"];
            model.code = code;
            model.customCode = defaultEle["customCode"];
            model.name = ele["name"];
            model.setting = ele["setting"];
            model.value = ele["value"];
            model.defaultValue = defaultEle["value"];
            // print("${model.code}, ${model.name}");

            if(model.code == "SC") {
              model.realValue = {"1C": "0", "℃": "1", "℉": "2"};
              model.value_show = {"0": "1C", "1": "℃", "2": "℉"};
            }else if(model.code == "DO") {
              model.realValue = {"OF": "5", "EL": "6", "HG": "7"};
              model.value_show = {"5": "OF", "6": "EL", "7": "HG"};
            }
            else if(model.code == "DM") {
              model.realValue = {"Off": "5", "Electric": "6", "Hot Gas": "7"};
              model.value_show = {"5": "Off", "6": "Electric", "7": "Hot Gas"};
            }
            else if(model.code == "FT" || model.code == "DS" || model.code == "AR" ||
                   model.code == "ID" || model.code == "T2" || model.code == "2N") {  // ["YES", "NO"]
              model.realValue = {"YES": "1", "NO": "0"};
              model.value_show = {"1": "YES", "0": "NO"};
            }else if(model.code == "2F") {  // 8-NO,9-OC,10-TC
              model.realValue = {"NO": "8", "OC": "9", "TC": "10"};
              model.value_show = {"8": "NO", "9": "OC", "10": "TC"};
            }else if(model.code == "T3") {
              model.realValue = {"NO": "11", "AU": "12", "FP": "13", "YES": "1", "NO": "0"};
              model.value_show = {"11": "NO", "12": "AU", "13": "FP", "1": "YES", "0": "NO"};
            }else if(model.code == "AO") {
              model.realValue = {"ON": "14", "PR": "15", "AL": "16", "DO": "17"};
              model.value_show = {"14": "ON", "15": "PR", "16": "AL", "17": "DO"};
            }

            List codeList = ["FT", "DS", "AR", "ID", "T2", "2F", "T3", "AO", "2N", "SC", "DO", "DM"];
            // 枚举数据, 布尔数据展示
            if(codeList.contains(model.code)) {
              if(model.code == "DM"){
                logs("DM value: ${model.value}, defaultValue: ${model.defaultValue}");
              }
              model.value = model.value_show[model.value] ?? "";
              model.defaultValue = model.value_show[model.defaultValue] ?? "";
            }

            _dataList.add(model);
          }

          if(_dataList.length == defaultParamsList.length) {
            // 删除customCode为空的数据, 即未配置数据点.
            // _dataList.removeWhere((element) => element.customCode.isEmpty);
            setState(() {});
            logs("内部刷新");
          }

        });
      });

    }else {
      SmartDialog.showToast(data.msg);
    }
  }

  // 批量设置参数
  _getParamsList2(String productId, ParamsModel model, String paramValue) async {
    final params = {"productId": "${productId}"};
    ResultData data = await AppApi.getInstance().getSettingInfo(params);
    if(data.code == 0){
      final paramsList = data.data["productSettingValue"] as List;
      paramsList.forEach((element) {
        final code = element["code"];
        final id = element["id"];
        if(code == model.code) {
          if(model.realValue.isNotEmpty){  // 枚举值, 布尔类型数据
            paramValue = model.realValue[paramValue].toString();
          }
          _updateDeviceParams(id, paramValue);
        }
      });
    }
  }


  _dealwithParams(ParamsModel model) async {

    model.code = model.code.toUpperCase();
    model.code = model.code.trim();
    logs("code: ${model}");
    /*
    if(model.code == "SC") {
              model.realValue = {"1℃": "0", "2℃": "1", "℉": "2"};
              model.value_show = {"0": "1℃", "1": "2℃", "2": "℉"};
            }else if(model.code == "DO") {
              model.realValue = {"OF": "5", "EL": "6", "HG": "7"};
              model.value_show = {"5": "OF", "6": "EL", "7": "HG"};
            }
            else if(model.code == "FT" || model.code == "DS" || model.code == "AR" ||
                   model.code == "ID" || model.code == "T2" || model.code == "2N") {  // ["YES", "No"]
              model.realValue = {"YES": "1", "No": "0"};
              model.value_show = {"1": "YES", "0": "No"};
            }else if(model.code == "2F") {  // 8-NO,9-OC,10-TC
              model.realValue = {"NO": "8", "OC": "9", "TC": "10"};
              model.value_show = {"8": "NO", "9": "OC", "10": "TC"};
            }else if(model.code == "T3") {
              model.realValue = {"NO": "11", "AU": "12", "FP": "13"};
              model.value_show = {"11": "NO", "12": "AU", "13": "FP"};
            }else if(model.code == "AO") {
              model.realValue = {"ON": "14", "PR": "15", "AL": "16", "DO": "17"};
              model.value_show = {"14": "ON", "15": "PR", "16": "AL", "17": "DO"};
            }

     */

    List<String> _valueList = [];
    var unitStr = "";
    if (model.code == "2N") {
      _valueList = ["YES", "NO"];
    } else if (model.code == "SC") {
      _valueList = ["1C", "℃", "℉"];
    } else if (model.code == "SL") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "SH") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "SP") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "HY") {
      unitStr = "℃";
      _valueList = _getNumList(-22, 20);
    } else if (model.code == "CR") {
      unitStr = "min";
      _valueList = _getNumList(0, 10);
    } else if (model.code == "CF") {
      unitStr = "min";
      _valueList = _getNumList(0, 10);
    } else if (model.code == "DF") {
      _valueList = _getNumList(0, 24);
    } else if (model.code == "DL") {
      unitStr = "℃";
      _valueList = _getNumList(0, 70);
    } else if (model.code == "DT") {
      unitStr = "min";
      _valueList = _getNumList(1, 99);
    } else if (model.code == "DM") {  // 5-0ff,6-Electric,7-Hot Gas
      _valueList = ["Off", "Electric", "Hot Gas"];
    } else if (model.code == "DO") {
      _valueList = ["OF", "EL", "HG"];
    } else if (model.code == "DR") {
      unitStr = "min";
      _valueList = _getNumList(0, 20);
    } else if (model.code == "DD") {
      unitStr = "min";
      _valueList = _getNumList(0, 20);
    } else if (model.code == "DY") {
      unitStr = "min";
      _valueList = _getNumList(0, 99);
    } else if (model.code == "FD") {
      unitStr = "min";
      _valueList = _getNumList(0, 10);
    } else if (model.code == "FT") {
      _valueList = ["YES", "NO"];
    } else if (model.code == "F1") {
      unitStr = "sec";
      _valueList = _getNumList(0, 180);
    } else if (model.code == "F2") {
      unitStr = "min";
      _valueList = _getNumList(0, 30);
    } else if (model.code == "F3") {
      unitStr = "min";
      _valueList = _getNumList(0, 31);
    } else if (model.code == "AL") {
      _valueList = _getNumList(-30, 0);
    } else if (model.code == "AH") {
      _valueList = _getNumList(0, 30);
    } else if (model.code == "AD") {
      unitStr = "min";
      _valueList = _getNumList(0, 120);
    } else if (model.code == "DS") {
      _valueList = ["YES", "NO"];   // 1-YES,0-NO
    } else if (model.code == "AS") {
      unitStr = "min";
      _valueList = _getNumList(0, 120);
    } else if (model.code == "AC") {
      unitStr = "wk";
      _valueList = _getNumList(0, 120);
    } else if (model.code == "AR") {
      _valueList = ["YES", "NO"];
    } else if (model.code == "EM") {
      unitStr = "min";
      _valueList = _getNumList(0, 99);
    } else if (model.code == "2L") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "2H") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "2S") {
      unitStr = "℃";
      _valueList = _getNumList(-40, 99);
    } else if (model.code == "2Y") {
      unitStr = "℃";
      _valueList = _getNumList(-22, 20);
    } else if (model.code == "2D") {
      _valueList = _getNumList(0, 24);
    } else if (model.code == "2F") {
      _valueList = ["NO", "OC", "TC"];  // 8-NO,9-OC,10-TC
    } else if (model.code == "ID") {
      _valueList = ["YES", "NO"];  // "1-YES,0-NO"
    } else if (model.code == "IC") {
      /// "setting": "0...99",
      unitStr = "min";
      final maxValue = model.setting.split('...').last;
      _valueList = _getNumList(0, int.tryParse(maxValue) ?? 0);
    } else if (model.code == "IT") {
      unitStr = "℃";
      _valueList = _getNumList(0, 30);
    } else if (model.code == "T2") {
      _valueList = ["YES", "NO"];   // 1-YES,0-NO
    } else if (model.code == "T3") {
      if(model.setting.contains('0') && model.setting.contains('1')){
        _valueList = ["YES", "NO"];
      }else{
        _valueList = ["NO", "AU", "FP"];  // {"11": "NO", "12": "AU", "13": "FP"};
      }
    } else if (model.code == "AO") {
      _valueList = ["ON", "PR", "AL", "DO"];
    } else if (model.code == "3S") {
      _valueList = _getNumList(-40, 90);
    } else if (model.code == "3H") {
      _valueList = _getNumList(-50, 1);
    } else if (model.code == "O1") {
      unitStr = "℃";
      _valueList = _getNumList(-20, 20);
    } else if (model.code == "O2") {
      unitStr = "℃";
      _valueList = _getNumList(-20, 20);
    } else if (model.code == "O3") {
      unitStr = "℃";
      _valueList = _getNumList(-20, 20);
    } else if (model.code == "TD") {
      unitStr = "min";
      _valueList = _getNumList(1, 30);
    } else if (model.code == "TS") {
      _valueList = _getNumList(0, 99);
    } else if (model.code == "SD") {
      _valueList = _getNumList(0, 99);
    } else if (model.code == "NR") {
      _valueList = _getNumList(0, 99);
    } else if (model.code == "CD") {
      _valueList = _getNumList(0, 10);
      unitStr = "min";
    }

    showModalBottomSheet<void>(
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        builder: (BuildContext context) {
          return UpdateParams(
            valueList: _valueList,
            unit: unitStr,
            selectedValue: model.value,
            onSelectedValue: (String selectedValue) {
              Navigator.pop(context);
              logs("选中的值: $selectedValue");

              // _updateDeviceParams(model.id, value);

              // 批量修改参数
              // _productIdList.forEach((ele) {
              //   _getParamsList2(ele, model, selectedValue);  // 获取参数列表, 匹配对应参数, 修改参数值
              // });

              // TySmartHome.updateDeviceDPS("6c7a7fd6565ac6f1edzn", "t01", 10);

              // 批量修改DP数据
              _productModelList.forEach((element) {
                var targetValue = "";

                if(model.realValue.isNotEmpty){
                  targetValue = model.realValue[selectedValue].toString();
                }else{
                  targetValue = selectedValue;
                }
                logs("targetValue: ${targetValue}");

                // 设置bool参数
                if(_containBoolCode(model)){
                  model.customCode = "bw_cfg_1";

                  final binaryValue = _getBinaryValue(model.code, targetValue, element);
                  targetValue = (int.parse(binaryValue, radix: 2) ?? 0).toString();
                }
                // return;

                // logs("修改设备: ${element.devId}, ${model.customCode}, ${targetValue}, ${int.tryParse(targetValue) ?? 0}");
                TySmartHome.updateDeviceDPS(element.devId, model.customCode.toLowerCase(), int.tryParse(targetValue) ?? 0).then((value) {
                  if(value == true) {   // 更改成功

                    _getParamsList2(element.productId, model, selectedValue);
                    SmartDialog.showToast("Success");
                  }else{
                    SmartDialog.showToast(LocaleKeys.add_device_config_fail.trc());
                  }
                });

                // /**
                //  * 现在所有数据点均为 value 类型
                //  */
                // final dpType = _getDpSchemaType(model.code);
                //
                // if(dpType == "value") {
                //   final targetValue = int.tryParse(value) ?? 0;
                //   TySmartHome.updateDeviceDPS(element.devId, model.code, targetValue);
                // }else if(dpType == "bool") {
                //   final targetValue = value == "Yes";
                //   TySmartHome.updateDeviceDPS(element.devId, model.code, targetValue);
                // }else if(dpType == "string" || dpType == "enum"){  // string 和 enum 内部都会转为 string
                //   TySmartHome.updateDeviceDPS(element.devId, model.code, value);
                // }

              });

            },
          );
    });

  }

  List<String> _getNumList(int startValue, int endValue) {
    List<String> valueList = [];
    for (int i = startValue; i <= endValue; i++) {
      valueList.add(i.toString());
    }
    return valueList;
  }

  // 修改设备参数
  _updateDeviceParams(String id, String value) async {
    final params = {"productSettingId": "${id}", "value": value};
    ResultData data = await AppApi.getInstance().updateParamValue(params);
    if(data.code == 0){
      _getParamsList(_productId);
    }else {
      SmartDialog.showToast(data.msg);
    }
  }

  // 获取设备DP类型
  _getDpSchemaType(String code) {
    final codeMap = {
      "n2" : "bool", "sc" : "enum", "sl" : "value", "sh" : "value",
      "sp" : "bool", "hy" : "value", "cr" : "value", "cd" : "value", "df" : "value",
      "dl" : "value", "dt" : "value", "do" : "enum", "dd" : "value", "dy" : "value",
      "fd" : "value", "ft" : "bool", "f1" : "value", "f2" : "value", "f3" : "value",
      "al" : "value", "ah" : "value", "ad" : "value", "ds" : "bool", "as" : "value",
      "ac" : 'value', "ar" : "bool", "em" : "value", "l2" : "value", "h2" : "value",
      "s2" : "value", "y2" : "value", "d2" : "value", 'ff2' : "enum", 'id' : "bool",
      'ic' : "value", 'it' : "value", 't2' : "bool", 't3' : "enum", 'ao' : "enum",
      "s3" : "value", 'h3' : "value", "t01" : "value", "t02" : "value", "t03" : 'value',
      "td" : "value", "sd" : "value", "nr" : "value", "mac" : "raw", "sn" : "string",
    };
    return codeMap[code] ?? "";
  }

  /// 是否包含控制参数
  _containBoolCode(ParamsModel model) {
    // 门开关, 智能除霜, 是否使用T2探头, 蜂鸣器再触发器, 蒸发风机控制
    return boolList.contains(model.code) && model.setting.contains('0') && model.setting.contains('1');
  }


  /// 获取涂鸦设备列表
  _getTyDeviceList() async {
    final deviceList = await TySmartHome.getDeviceList();
    if(deviceList.isEmpty){
      showOkCancelAlertDialog(
          context: context,
          message: LocaleKeys.other_wifi_tip.trc(),
          cancelLabel: LocaleKeys.login_cancel.trc(),
          okLabel: LocaleKeys.other_wifi_config.trc(),
          builder: (context, child) => Theme(
          data: ThemeData(
              primarySwatch: createMaterialColor(isDarkMode ? Colors.white : Colors.black),
              dialogBackgroundColor: isDarkMode ? MainCommon.darkBlueMaterialColor : Colors.white,
              colorScheme: ColorScheme(
                primary: isDarkMode ? Colors.white : Colors.black, // 文字颜色
                primaryVariant: Colors.white,
                secondary: Colors.green,
                background: Colors.white,
                error: Colors.red,
                brightness: isDarkMode ? Brightness.dark : Brightness.light,    /// 最最关键
                onBackground: Colors.pink,
                secondaryVariant: Colors.white,
                onError: Colors.yellow,
                onPrimary: Colors.red, //顶部栏字体颜色
                onSecondary: Colors.black,
                onSurface: Colors.white,   // 导航栏字体颜色, 返回按钮颜色
                surface: MainCommon.darkBlueMaterialColor,  // 导航栏背景颜色
              )),
          child: child,
        ),

      ).then((value) {
        if(value == OkCancelResult.ok){
          Get.offNamed(LCRouter.wifi_page);
        }
      });

      return;
    }

    for (var value in _productIdList) {

      final params = {"productId": value.toString()};
      ResultData data = await AppApi.getInstance().getProductInfo(params);
      if(data.code == 0){
        var model = ProductModel.fromJson(data.data);
        deviceList.forEach((ele) {
          logs("涂鸦设备: ${ele}");
          final sn = ele["dps"]["102"];
          if(sn == model.serialNumber) {  // 只能修改已添加存在的设备
            model.devId = ele["devId"];
            model.productId = value;
          }
        });
        _productModelList.add(model);
        /*
        {
          devId: 6c51ab9825096c87385dml,
          dps: {
              110: 8, 111: 8, 112: 40, 113: 6, 135: 27, 114: 17, 136: 12, 115: 13, 137: 10, 116: 4,
              138: 27, 117: 1b1ad70400010006020001ff0508082806110d040001060202080a0c0a000000050aff000303151d190000000000,
              139: 21, 118: 0, 119: 3, 140: 17, 141: 0, 120: 3, 142: 0, 121: 0, 143: 0, 122: 0, 144: 0, 101: false, 123: 0, 145: 0,
              102: williams95573, 124: 4, 146: 5, 103: 1, 125: 0, 147: 10, 104: 0, 126: 0, 105: 6, 127: 0, 106: 2, 107: 0, 108: 1, 109: 5
              },
          homeId: 66146382,
          isOnline: true,
          name: 冷凝器
        }
         */

      }


    }


  }

  // 通过 配置寄存器	bw_cfg_1 参数点, 处理门开关, 智能除霜, 是否使用T2探头, T3探头, 蜂鸣器触发器, 蒸发机控制
  _getBinaryValue(String code, String targetValue, ProductModel productModel) {
    var dsValue = "";
    var idValue = "";
    var t2Value = "";
    var arValue = "";
    var ftValue = "";
    var t3Value = "";
    var n2Value = "";

    _dataList.forEach((paramModel) {
      final tempValue = paramModel.value == "YES" ? "1" : "0";
      switch (paramModel.code) {
        case "DS":
          dsValue = tempValue;break;
        case "ID":
          idValue = tempValue;break;
        case "T2":
          t2Value = tempValue;break;
        case "AR":
          arValue = tempValue;break;
        case "FT":
          ftValue = tempValue;break;
        case "T3":
          t3Value = tempValue;break;
        case "2N":
          n2Value = tempValue;break;
      }
    });

    var tempValue = targetValue; // == "YES" ? "1" : "0";
    switch (code) {
      case "DS":
        dsValue = tempValue;break;
      case "ID":
        idValue = tempValue;break;
      case "T2":
        t2Value = tempValue;break;
      case "AR":
        arValue = tempValue;break;
      case "FT":
        ftValue = tempValue;break;
      case "T3":
        t3Value = tempValue;break;
      case "2N":
        n2Value = tempValue;break;
    }
    var binaryValue = "";
    if(productModel.controllerModelName == "WUBC3"){
      binaryValue = "000" + t2Value + idValue + dsValue + t3Value + ftValue;
    }else if(productModel.controllerModelName == "WUMC3"){
      binaryValue = "0" + t2Value + arValue + n2Value + idValue + dsValue + "0" + ftValue;
    }
    // logs("binaryValue: $binaryValue === code: ${code} === dsValue: ${dsValue}");
    return binaryValue;
  }

}
